
export default {
	namespaced:true,
	
	
	state: () => ({
		car_list: JSON.parse(uni.getStorageSync('car_list') || '[]')
	}),
	
	actions: {
		
	},
	mutations: {
		// 删除已支付的商品
		del_pay_ok_goods(state, data) {
			state.car_list = data;
			this.commit('m_shopCar/set_data');
		},
		
		add_car_list(state, data) {
			const goods = state.car_list.find(item =>  data.goods_id === item.goods_id);
			if(!goods) {
				state.car_list.push(data)
			}else {
				goods.goods_count ++ 
			}
			this.commit('m_shopCar/set_data');
		},
		
		goods_count_chang(state, {count, goods}) {
			const fgoods = state.car_list.find((item) => {
				return item.goods_id === goods.goods_id
			})
			fgoods.goods_count = count
			this.commit('m_shopCar/set_data');
		},
		
		goods_state_chang(state, {id, g_state}) {
			const fgoods_2 = state.car_list.find((item) => {
				return item.goods_id === id
			})
			fgoods_2.goods_state = g_state
			this.commit('m_shopCar/set_data');
			// console.log((state.car_list[0].goods_count -0) * (state.car_list[0].goods_price -0));
		},
		
		del_shopCar_goods(state, id) {
			const del_index = state.car_list.findIndex((item, index) => {
				return item.goods_id === id
			})
			state.car_list.splice(del_index, 1)
			this.commit('m_shopCar/set_data');
		},
		
		all_radio_change(state, new_state) {
			state.car_list.forEach((item) => {
				item.goods_state = new_state;
			});
			this.commit('m_shopCar/set_data');
		},
		
		set_data(state) {
			uni.setStorageSync('car_list', JSON.stringify(state.car_list));
		}
	},
	getters: {
		goods_totle(state) {
			return state.car_list.reduce((before, item) => {
				return before += parseInt(item.goods_count);
			}, 0)
		},
		settle_num(state) {
			return state.car_list.filter(item => item.goods_state).reduce((before, item) => {
				return before += (item.goods_count -0)
			}, 0)	
		},
		settle_price_all(state) {
			return state.car_list.filter(item => item.goods_state).reduce((before, item) => {
				return before += ((item.goods_count -0) * (item.goods_price -0))
			}, 0)
		}
	}
}